package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.PermissionService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.LOrderbillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.web.service.TokenService;

import java.util.List;

@RestController
@RequestMapping("/wechat")
public class WeChatController extends BaseController {

    @Autowired
    private ILGuestService lGuestService;
    @Autowired
    private ILGoodsService lGoodsService;
    @Autowired
    private ILOrderbillService lOrderbillService;
    @Autowired
    private ILMessageService lMessageService;
    @Autowired
    private ILRechargeService lRechargeService;
    @Autowired
    private ILWaybillService lWaybillService;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private PermissionService permissionService;

    //微信接口
    //客户信息
    @GetMapping("/getinfo")
    public AjaxResult wxGetInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        return AjaxResult.success(lGuestService.selectByUserId(loginUser.getUserId()));
    }

    @PostMapping("/order")
    public AjaxResult add(@RequestBody LOrderbill lOrderbill) {
        return toAjax(lOrderbillService.insertLOrderbill(lOrderbill));
    }


    @GetMapping("/order/list")
    public TableDataInfo orderList(LOrderbill lOrderbill) {
        if (lOrderbill.getGid() == null) {
            return getDataTableError("获取数据失败，code：-1");
        }
        startPage();
        List<LOrderbillVo> list = lOrderbillService.selectLOrderbillVoList(lOrderbill);
        return getDataTable(list);
    }

    @GetMapping("/goods/list")
    public AjaxResult listAll() {
        List<LGoods> list = redisCache.getCacheList("lGoods");
        if (list.size() == 0) {
            LGoods lGoods = new LGoods();
            lGoods.setClosed(0L);
            list = lGoodsService.selectLGoodsList(lGoods);
            redisCache.setCacheList("lGoods", list);
        }

        return AjaxResult.success(list);
    }

    /**
     * 获取消息
     */

    @GetMapping("/message/list")
    public TableDataInfo list(LMessage lMessage) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        startPage();
        lMessage.setTogid(loginUser.getUserId());
        lMessage.setClosed(0);

        List<LMessage> list = lMessageService.selectLMessageList(lMessage);
        return getDataTable(list);
    }

    /**
     * 获取消息详情
     */

    @GetMapping("/message/info")
    public AjaxResult read(LMessage lMessage) {
        lMessage =lMessageService.selectLMessageByMid(lMessage.getMid());
        if (lMessage.getIsbrowse()!=1) {
            LMessage paramMessage = new LMessage();
            paramMessage.setIsbrowse(1);
            paramMessage.setMid(lMessage.getMid());
            lMessageService.updateLMessage(paramMessage);
        }
        return AjaxResult.success(lMessage);
    }


    /**
     * 充值订单
     */
    @GetMapping("/recharge/list")
    public TableDataInfo rechargeList(LRecharge lRecharge) {
        if (lRecharge.getGid() == null) {
            return getDataTableError("获取数据失败，code：-1");
        }
        startPage();
        List<LRecharge> list = lRechargeService.selectLRechargeList(lRecharge);
        return getDataTable(list);
    }

    /**
     * 充值订单
     */
    @GetMapping("/waybill/list")
    public TableDataInfo waybillList(LWaybill lWaybill) {
        if (lWaybill.getGid() == null) {
            return getDataTableError("获取数据失败，code：-1");
        }
        startPage();
        List<LWaybill> list = lWaybillService.selectLWaybillList(lWaybill);
        return getDataTable(list);
    }
    /*
     * 修改密码
     * */

    @GetMapping("/user/updatePwd")
    public AjaxResult updatePwd(String oldPassword, String newPassword) {
        LoginUser loginUser = getLoginUser();
        String userName = loginUser.getUsername();
        String password = loginUser.getPassword();
        if (!SecurityUtils.matchesPassword(oldPassword, password)) {
            return AjaxResult.error("修改密码失败，旧密码错误");
        }
        if (SecurityUtils.matchesPassword(newPassword, password)) {
            return AjaxResult.error("新密码不能与旧密码相同");
        }
        if (userService.resetUserPwd(userName, SecurityUtils.encryptPassword(newPassword)) > 0) {
            // 更新缓存用户密码
            loginUser.getUser().setPassword(SecurityUtils.encryptPassword(newPassword));
            tokenService.setLoginUser(loginUser);
            return AjaxResult.success();
        }
        return AjaxResult.error("修改密码异常，请联系管理员");
    }

    @GetMapping("/check/role")
    public AjaxResult updatePwd(String role) {
        return AjaxResult.success(permissionService.hasPermi(role));
    }

}
